<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Adminhtml
 * @copyright  Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Adminhtml sales orders controller
 *
 * @author      Magento Core Team <core@magentocommerce.com>
 */
//require_once("Mage/Adminhtml/controllers/Sales/CreditmemoController.php");

class Silk_Zgb_Adminhtml_Sales_CreditmemoController extends Mage_Adminhtml_Controller_Sales_Creditmemo
{
    /**
     * Export credit memo grid to CSV format
     */
    public function exportCsvAction()
    {
        $fileName   = 'creditmemos.csv';
        $grid       = $this->getLayout()->createBlock('adminhtml/sales_creditmemo_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
    }

    /**
     *  Export credit memo grid to Excel XML format
     */
    public function exportExcelAction()
    {
        $fileName   = 'creditmemos.xml';
        $grid       = $this->getLayout()->createBlock('adminhtml/sales_creditmemo_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
    }

    /**
     *  Index page
     */
    public function indexAction() {
        $this->_title($this->__('Sales'))->_title($this->__('Credit Memos'));

        parent::indexAction();
    }

    /**
     * Save creditmemo and related order, invoice in one transaction
     * @param Mage_Sales_Model_Order_Creditmemo $creditmemo
     */
    protected function _saveCreditmemo($creditmemo)
    {
        $transactionSave = Mage::getModel('core/resource_transaction')
            ->addObject($creditmemo)
            ->addObject($creditmemo->getOrder());
        if ($creditmemo->getInvoice()) {
            $transactionSave->addObject($creditmemo->getInvoice());
        }
        $transactionSave->save();

        return $this;
    }

    public function returnedAction()
    {
        $id = $this->getRequest()->getParam('creditmemo_id');
        $memo = Mage::getModel('sales/order_creditmemo')->load($id);
        if($memo->getState()==Mage_Sales_Model_Order_Creditmemo::STATE_OPEN)
        {
            Mage::unregister('current_creditmemo');
            Mage::register('current_creditmemo', $memo);
            Mage::register('aitoc_order_refund_store_id', $memo->getOrder()->getStoreId());
            //确认已退货
            foreach ($memo->getAllItems() as $memoItem) {
                $memoItem->setBackToStock(true);
            }
            $this->_getSession()->addSuccess("操作成功！");
            if($memo->getSource()==1)
            {
                $memo->setState(Mage_Sales_Model_Order_Creditmemo::STATE_REFUNDED)->save();
            }
            else
            {
                $memo->setState(Mage_Sales_Model_Order_Creditmemo::STATE_RETURN)->save();
            }
        }
        else
        {
            $this->_getSession()->addError("状态不正确！");
        }
        $this->_redirect('*/*/');
    }

    public function refundAction()
    {
        $id = $this->getRequest()->getParam('creditmemo_id');
        $memo = Mage::getModel('sales/order_creditmemo')->load($id);

        if($memo->getState() == Mage_Sales_Model_Order_Creditmemo::STATE_RETURN && !$memo->getSource())
        {

			
			$this->_getSession()->addSuccess("退款操作成功！");
			$memo->setState(Mage_Sales_Model_Order_Creditmemo::STATE_REFUNDED)->save();
   
			
        }
        else
        {
            $this->_getSession()->addError("状态不正确！");
        }
        $this->_redirect('*/*/');
    }

    public function massStatusAction()
    {
        $ids = $this->getRequest()->getParam('creditmemo_ids');
        $status = $this->getRequest()->getParam('status');
        $cid = array();
        foreach($ids as $id)
        {
            $memo = Mage::getModel('sales/order_creditmemo')->load($id);

            if($memo && $memo->getId())
            {
                Mage::unregister('current_creditmemo');
                Mage::register('current_creditmemo', $memo);
                if($status == Mage_Sales_Model_Order_Creditmemo::STATE_RETURN && $memo->getState()!=Mage_Sales_Model_Order_Creditmemo::STATE_OPEN)
                {
                    continue;
                }

                if($status == Mage_Sales_Model_Order_Creditmemo::STATE_REFUNDED && $memo->getState()!=Mage_Sales_Model_Order_Creditmemo::STATE_RETURN)
                {
                    continue;
                }

                Mage::unregister('aitoc_order_refund_store_id');
                Mage::register('aitoc_order_refund_store_id', $memo->getOrder()->getStoreId());
                //确认已退货
                if($status == Mage_Sales_Model_Order_Creditmemo::STATE_RETURN)
                {
                    foreach ($memo->getAllItems() as $memoItem) {
                        $memoItem->setBackToStock(true);
                    }
                }

                

                if($status == Mage_Sales_Model_Order_Creditmemo::STATE_RETURN && $memo->getSource())
                {
                    $status = Mage_Sales_Model_Order_Creditmemo::STATE_REFUNDED;
                }
                $memo->setState($status)->save();
                $cid[] = $memo->getId();
              
            }
        }

        if(count($cid)<count($ids))
        {
            $this->_getSession()->addError(
                $this->__('所选的订单中有部分不符合操作条件！')
            );
            $this->_getSession()->addSuccess(
                $this->__('共%d个退款/退货单操作成功！.', count($cid))
            );
        }
        else
        {
            $this->_getSession()->addSuccess(
                $this->__('共%d个退款/退货单操作成功！.', count($cid))
            );
        }

        
        $this->_redirect('*/*/');
    }
}
